<?php

namespace humhub\modules\api\controllers;
use humhub\modules\api\models\Space;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\content\models\Wall;
use humhub\modules\space\models\Membership;
use Yii;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use humhub\modules\activity\models\Activity;
use humhub\modules\api\models\Content;
use humhub\modules\api\models\Follow;
use humhub\modules\content\models\WallEntry;
use humhub\modules\file\models\File;
use humhub\modules\post\models\Post;
/**
 * 删除资料
 */
class DeleteController extends Base4Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        return $behaviors;
    }

    /**
     * 删除指定图文 任务  注意！！space id 是否需要填写！！不建议删除任务 附带讨论组无法删除
     *
     * GET api/delete/delete
     *
     * get.id post id (必传）
     *
     * get.sid space id （选传，有则删除任务，无则删除图文，不建议传）
     *
     * @return boolean
     */
    public function actionDelete()
    {
        $pid=Yii::$app->request->get('id');
        $sid=Yii::$app->request->get('sid');
        $uid=Yii::$app->user->id;
        if ($sid){
            $space=Space::findOne($sid);
            if ($space){
                if ($uid !=$space->created_by) throw new \yii\web\BadRequestHttpException('无权删除');;
                $activity=Activity::findOne(['object_id'=>$sid]);
                if ($activity){
                    $aid=$activity->id;
                    $cc=Content::findOne(['object_id'=>$aid]);
                    $cid=$cc->id;$wid=$cc->contentcontainer_id;

                    WallEntry::deleteAll(['content_id'=>$cid]);
                    Content::deleteAll(['id'=>$cid]);Activity::deleteAll(['id'=>$aid]);
                    Wall::deleteAll(['object_id'=>$sid]);Membership::deleteAll(['space_id'=>$sid]);
                    ContentContainer::deleteAll(['id'=>$wid]);
                    Space::deleteAll(['id'=>$sid]);
                }
            }
        }
        $post=Post::findOne($pid);
        if($post){
            if ($uid !=$post->created_by) throw new \yii\web\BadRequestHttpException('无权删除');;
            $activity=Activity::findOne(['object_id'=>$pid]);
            if($activity){
                $aid=$activity->id;
                $cid1=Content::findOne(['object_id'=>$aid])->id;
                WallEntry::deleteAll(['content_id'=>$cid1]);
                Content::deleteAll(['id'=>$cid1]);Activity::deleteAll(['id'=>$aid]);
            }

            $content=Content::findOne(['object_id'=>$pid]);
            if ($content){
                $cid2=$content->id;
                WallEntry::deleteAll(['content_id'=>$cid2]);
                Content::deleteAll(['id'=>$cid2]);
            }
            File::deleteAll(['object_id'=>$pid]);
            Follow::deleteAll(['object_id'=>$pid]);
            $post->delete();
            return true;
        }
        return false;
    }
}

